package com.jlkj.web.shopnew.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.jlkj.web.shopnew.controller.base.BaseController;
import com.jlkj.web.shopnew.core.ResultCode;
import com.jlkj.web.shopnew.core.StatusCode;
import com.jlkj.web.shopnew.pojo.*;
import com.jlkj.web.shopnew.request.*;
import com.jlkj.web.shopnew.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/1.0")
public class StaffController extends BaseController {

    @Autowired
    private IStaff staffService;
    @Autowired
    private ICompany companyService;

    @Autowired
    private ICompanyInvite companyInviteService;

    @Autowired
    private IUser userService;

    @Autowired
    private IManagers managersService;


    //拒绝
    @RequestMapping(value = "/saveStaff" , method = RequestMethod.POST)
    @Transactional
    public ResultCode saveStaff(@RequestBody SaveStaffRequest saveStaffRequest){

        User user = userService.selectByPrimaryKey(saveStaffRequest.getUid());

        CompanyInvite condi = new CompanyInvite();
        condi.setUid(user.getId());
        condi.setFlag(0);
        condi.setBossId(saveStaffRequest.getBossId());

        CompanyInvite update = new CompanyInvite();
        update.setFlag(saveStaffRequest.getType());
        companyInviteService.updateByCondition(update,condi);
        if(saveStaffRequest.getType() == 2){//拒绝，就修改comany_invite 表flag=2 结束
            return new ResultCode(StatusCode.SUCCESS);
        }

        Staff bean = new Staff();
        bean.setStaffId(saveStaffRequest.getUid())
                .setStaffName(user.getUsername())
                .setCompanyId(saveStaffRequest.getCompanyId())
                .setStaffPic(user.getImg())
                .setStaffPosition(user.getPosition());

        int i = staffService.insert(bean);

        if(i != 1){
            return new ResultCode(StatusCode.ERROR);
        }
        return new ResultCode(StatusCode.SUCCESS);

    }

    //查询是否有邀请
    @RequestMapping(value = "companyInvite" , method = RequestMethod.POST)
    public ResultCode companyInvite(@RequestBody GetCompanyInviteRequest getCompanyInviteRequest){
        CompanyInvite condi = new CompanyInvite();
        condi.setUid(getCompanyInviteRequest.getUid());
        condi.setFlag(0);
        CompanyInvite bean = companyInviteService.getByCondition(condi);

        if(bean != null){
            Company company = companyService.selectByPrimaryKey(bean.getCompanyId());
            bean.setCompanyName(company == null ? "" : company.getCompanyName());

            User boss = userService.selectByPrimaryKey(bean.getBossId());

            bean.setBossName(boss == null ? "" : boss.getUsername());

        }

        return new ResultCode(StatusCode.SUCCESS,bean);
    }


    //获取我的所有员工
    @RequestMapping(value = "/getStaff" , method = RequestMethod.POST)
    public ResultCode getStaff(){
        JSONObject json = getJsonFromRequest();
        int bossId = json.getInteger("bossId");

        Company condi = new Company();
        condi.setBossId(bossId);
        Company company = companyService.getByCondition(condi);
        List<Staff> list = Lists.newArrayList();
        if(company != null) {
            Staff cond = new Staff();
            cond.setCompanyId(company.getId());
            list = staffService.getList(cond);
        }
        return new ResultCode(StatusCode.SUCCESS,list);
    }

    //统计员工排行
    @RequestMapping(value = "/staffSort" , method = RequestMethod.POST)
    public ResultCode staffSort(@RequestBody GetStaffSortRequest getStaffSortRequest){
        List<StaffDto> list = staffService.staffSort(getStaffSortRequest);
        return new ResultCode(StatusCode.SUCCESS , list);
    }


    @RequestMapping(value = "/staffAnalysis" , method = RequestMethod.POST)
    public ResultCode staffAnalysis(@RequestBody GetStaffAnalysisRequest getStaffAnalysisRequest){
        List<StaffAnalysisDto> list = staffService.staffAnalysis(getStaffAnalysisRequest);
        return new ResultCode(StatusCode.SUCCESS,list);
    }


    @RequestMapping(value = "/deleteStaff" , method = RequestMethod.POST)
    @Transactional
    public ResultCode deleteStaff(@RequestBody DeleteStaffRequest deleteStaffRequest){
        boolean flag = staffService.deleteStaff(deleteStaffRequest);

        User condi = new User();
        condi.setId(deleteStaffRequest.getUid());
        User update = new User();
        update.setType(1);
        update.setPayTime(0L);
        update.setParentId(191);
        update.setSource(1);

        userService.updateByCondition(update,condi);


        //人数减1
        int companyId = companyService.getCompanyId(deleteStaffRequest.getBossId());
        Company company = companyService.selectByPrimaryKey(companyId);

        int employeeNumAbled = company.getEmployeeNumAbled() - 1;
        Company condiCompany = new Company();
        condiCompany.setId(companyId);
        Company updateCompany = new Company();
        updateCompany.setEmployeeNumAbled(employeeNumAbled);
        companyService.updateByCondition(updateCompany,condiCompany);


        try {
            Managers cond = new Managers();
            cond.setUid(deleteStaffRequest.getUid());

            Managers updateM = new Managers();
            updateM.setStatus(0);

            managersService.updateByCondition(updateM, cond);
        }catch (Exception e){

        }
        if(!flag){
            return new ResultCode(StatusCode.ERROR);
        }
        return new ResultCode(StatusCode.SUCCESS);
    }
}
